package com.hzyb.shopOrder.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hzyb.shopOrder.service.ShopOrderService;

@RestController
@RequestMapping("/shopOrder")
public class ShopOrderController {

	private static final Logger Logger=LoggerFactory.getLogger(ShopOrderController.class);

	@Autowired
	private ShopOrderService ShopOrderService;

	@RequestMapping("/createOrder")
	public Map<String, Object> createOrder(String cartId,String shopOrder){
		Map<String, Object> map=new HashMap<>();
		map.put("status", "fail");
		try {
			if(StringUtils.isEmpty(cartId)&&StringUtils.isEmpty(shopOrder)) {
				map.put("error_msg","cartId&shopOrder all null!!");
				return map;
			}
			//如果购物车id不为空，则从购物车生成订单，否则从传入的json串解析生成
			if(!StringUtils.isEmpty(cartId)) {
				map.put("data", ShopOrderService.createShopOrderfromShopCart(cartId));
			}else if (!StringUtils.isEmpty(shopOrder)) {
				map.put("data", ShopOrderService.createShopOrder(JSONObject.parseObject(shopOrder)));
			}
			map.put("status", "success");
		} catch (Exception e) {
			Logger.error(e.toString());
			map.put("error_msg", e.getMessage());
		}
		return map;
	}

	@RequestMapping("/queryOrderInformation")
	public Map<String, Object> queryOrderInformation(@RequestParam(required=true) String orderId){
		Map<String, Object> map=new HashMap<>();
		map.put("status", "fail");
		try {
			map.putAll(ShopOrderService.queryOrderInformation(orderId));
			map.put("status", "success");
		} catch (Exception e) {
			Logger.error(e.toString());
			map.put("error_msg", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/orderCancel")
	public Map<String, Object> orderCancel(@RequestParam String orderId){
		Map<String, Object> map=new HashMap<>();
		map.put("status", "fail");
		try {
			ShopOrderService.orderCancel(orderId);
			map.put("status", "success");
		} catch (Exception e) {
			Logger.error(e.toString());
			map.put("error_msg", e.getMessage());
		}
		return map;
	}
	
	@RequestMapping("/remindShipment")
	public Map<String, Object> remindShipment(@RequestParam String orderId){
		Map<String, Object> map=new HashMap<>();
		map.put("status", "fail");
		try {
			ShopOrderService.remindShipment(orderId);
			map.put("status", "success");
		} catch (Exception e) {
			Logger.error(e.toString());
			map.put("error_msg", e.getMessage());
		}
		return map;
	}

}
